package cn.lopr.im.socketserver.utils;


import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class HttpUtil {

    public static Call doPost(String url, File file, Map<String, String> parameter) {
        RequestBody requestBody = null;

        if (!((parameter == null || parameter.size() == 0) && file == null)) {
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);

            if (file != null) {
                // MediaType.parse() 里面是上传的文件类型。
                RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
                String filename = file.getName();
                // 参数分别为， 请求key ，文件名称 ， RequestBody
                if (url.contains("update")) {
                    builder.addFormDataPart("updata_file", filename, body);
                } else {
                    builder.addFormDataPart("upload_file", filename, body);
                }

            }

            if (parameter != null) {
                for (String key : parameter.keySet()) {
                    String value = parameter.get(key);
                    builder.addFormDataPart(key, value);
                }
            }

            requestBody = builder.build();

        } else {
            requestBody = new FormBody.Builder().build();
        }


        Request requestPost = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        return OkHttp.getInstance().newCall(requestPost);
    }


    public static void doPostForResult(String url, File file, Map<String, String> parameter, final OnRequestResult listener) {
        Call call = doPost(url, file, parameter);
        if (call != null) {
            call.enqueue(new Callback() {
                public void onFailure(Call call, IOException e) {
                    if (listener != null) {
                        listener.onResult("");
                    }
                }

                public void onResponse(Call call, Response response) throws IOException {
                    String data = "";
                    if (response.isSuccessful()) {
                        data = response.body().string();
                    }
                    call.cancel();
                    final String result = data;
                    if (listener != null)
                        listener.onResult(result);

                }
            });
        }

    }


    public static void doGetForResult(String url, Map<String, String> params, final OnRequestResult listener) {
        if (params != null && params.size() != 0) {
            url += "?";
            StringBuilder urlBuilder = new StringBuilder(url);
            for (String key : params.keySet()) {
                String value = params.get(key);
                urlBuilder.append(key).append("=").append(value).append("&");
            }
            url = urlBuilder.toString();
        }
        Request request = new Request.Builder()
                .url(url)
                .build();
        OkHttp.getInstance().newCall(request).enqueue((new Callback() {
            public void onFailure(Call call, IOException e) {
                if (listener != null) {
                    listener.onResult("");
                }
            }

            public void onResponse(Call call, Response response) throws IOException {
                String data = "";
                if (response.isSuccessful()) {
                    data = response.body().string();
                }
                call.cancel();
                final String result = data;
                if (listener != null)
                    listener.onResult(result);

            }
        }));

    }

    public interface OnRequestResult {
        void onResult(String data);
    }

}
